package com.jiamao.goplay.ui.main.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;

import com.google.android.material.appbar.AppBarLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.ConvertUtils;

/**
 * Created by goldze on 2018/7/18.
 *
 *  下拉滚动 appbar滚动 Toolbar显示
 */

public class TabBar5Fragment extends BaseFragment {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_5;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    private int mScrollY = 0;
    @Override
    public void initData() {
        super.initData();

       // ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.mIv));
        ImmersionBar.setTitleBar(this, binding.getRoot().findViewById(R.id.toolbar));

        //设置ToolBar
//        //  ImmersionBar.titleBarMarginTop(this,  binding.getRoot().findViewById(R.id.toolbar));
//        SmartRefreshLayout swipeRefreshLayout = (SmartRefreshLayout)  binding.getRoot().findViewById(R.id.refresh);
//
//
//         AppBarLayout appBarLayout = (AppBarLayout)  binding.getRoot().findViewById(R.id.app_bar);
//       // swipeRefreshLayout.setEnableRefresh(false);
//        View mAppBarChildAt = appBarLayout.getChildAt(0);
//        AppBarLayout.LayoutParams  mAppBarParams = (AppBarLayout.LayoutParams)                  mAppBarChildAt.getLayoutParams();
//        mAppBarParams.setScrollFlags(0);
//
//        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                mAppBarParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
//                mAppBarChildAt.setLayoutParams(mAppBarParams);
//            }
//        });
//        appBarLayout.addOnOffsetChangedListener((appBarLayout1, verticalOffset) -> {
//
//            Log.d("HTML:"," VER:"+verticalOffset);
//
//            if (verticalOffset < 0 ){
//                swipeRefreshLayout.setEnableRefresh(false);
//            }else {
//                swipeRefreshLayout.setEnableRefresh(true);
//            }
//
//
//            //  ImmersionBar.with(this).statusBarDarkFont(Math.abs(i) >= offHeight, 0.2f).init();
//
//        });

        Toolbar  linearLayout = binding.getRoot().findViewById(R.id.toolbar);
        // CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)  binding.getRoot().findViewById(R.id.toolbar_layout);
        NestedScrollView scrollView =  binding.getRoot().findViewById(R.id.item_detail_container);
        Button button = binding.getRoot().findViewById(R.id.btntext);
        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = ConvertUtils.dp2px(80);
            int color = ContextCompat.getColor(getContext(), R.color.orange) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int[] location = new int[2];
                //  magicIndicator.getLocationOnScreen(location);
                //   int xPosition = location[0];
                //   int yPosition = location[1];
                Log.d("HTML","lastScrollY:"+lastScrollY+" H:"+h+" mScrollY:"+mScrollY+" oldScrollY:"+oldScrollY+" scrollY:"+scrollY);

                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
                    linearLayout.setAlpha(1f * mScrollY / h);
                    linearLayout.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
                    // ivHeader.setTranslationY(mOffset - mScrollY);
                }else {
                    // linearLayout.setAlpha(0);
                    // linearLayout.setBackgroundColor(0);
                }
                if (scrollY == 0) {
                    button.setText("白色");
                    linearLayout.setAlpha(255);
                    linearLayout.setBackgroundColor(0);
                    linearLayout.setVisibility(View.GONE);
                    //   ivBack.setImageResource(R.drawable.back_white);
                    // ivMenu.setImageResource(R.drawable.icon_menu_white);
                } else {
                    linearLayout.setVisibility(View.VISIBLE);
                    button.setText("黑色");
                    linearLayout.setBackgroundColor(Color.WHITE);
                    //      ivBack.setImageResource(R.drawable.back_black);
                    //  ivMenu.setImageResource(R.drawable.icon_menu_black);
                }

                lastScrollY = scrollY;
            }
        });


//        collapsingToolbarLayout.post(() -> {
//
//
//
//            int offHeight = collapsingToolbarLayout.getHeight() - ImmersionBar.getStatusBarHeight(this);
//            appBarLayout.addOnOffsetChangedListener((appBarLayout1, i) -> {
//                ImmersionBar.with(this).statusBarDarkFont(Math.abs(i) >= offHeight, 0.2f).init();
//            });
//        });


        //ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.toolbar));
    }


}
